package com.infobie.android.pocketdhamma.dhammapada;

import gueei.binding.app.BindingActivity;
import gueei.binding.Command;
import gueei.binding.Observable;
import gueei.binding.observables.IntegerObservable;
import gueei.binding.cursor.CursorObservable;

import com.infobie.android.pocketdhamma.R;
import com.infobie.android.pocketdhamma.dhammapada.DB.*;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

@SuppressWarnings("deprecation")
public class Main extends BindingActivity {
	DBHelper dbHelper = null;
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.main); // default
        this.setAndBindRootView(R.layout.main, this);
        this.setAndBindOptionsMenu(R.menu.mainmenu, this);
        
        try
        {
	        dbHelper = new DBHelper(this);
	        Cursor c = dbHelper.GetAllViewChapters();
	        ChapterList.setCursor(c);
        }
        catch(Exception ex)
        {
        	Alerts.Show(this, "Error", ex.getMessage());
        }
    }
	
	@Override
	public void onDestroy() {
		super.onDestroy();
		dbHelper.close();
	}

	public IntegerObservable ImageDharmaWheel = new IntegerObservable(R.drawable.ic_dharma_wheel);
	public final CursorObservable<ChapterItem> ChapterList = new CursorObservable<ChapterItem>(ChapterItem.class);
    public final Observable<Object> ChapterItemClicked = new Observable<Object>(Object.class);
    public final Command ChapterItemClickedCommand = new Command(){
            public void Invoke(View view, Object... args) {
            	ChapterItem chapter = (ChapterItem)ChapterItemClicked.get();
            	String sText = "Chapter " + chapter.ChapterId.get().toString() + ". " + chapter.ChapterName.get();
                Toast.makeText(view.getContext(), sText, Toast.LENGTH_SHORT).show();
                
                Intent intent = new Intent(Main.this, VerseList.class);
                intent.putExtra("ChapterName", chapter.ChapterName.get());
                intent.putExtra("ChapterId", chapter.ChapterId.get());
                Main.this.startActivity(intent);
            }
    };
    
    public final Command btnAboutClicked = new Command(){
            @Override
            public void Invoke(View view, Object... args) {
            	Alerts.ShowAbout(Main.this);
            }
    };
}